using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using System.Globalization;


namespace Qreed.Xna
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class FrameRateComponent : DrawableGameComponent
    {
        private TimeSpan _elapsedTime = TimeSpan.Zero;
        private int _frameRate;
        private int _frameCounter;

        private NumberFormatInfo _format;

        private SpriteBatch _spriteBatch;
        private SpriteFont _spriteFont;

        /// <summary>
        /// The position where to render the text
        /// </summary>
        public Vector2 Position = new Vector2(5.0f, 5.0f);

        /// <summary>
        /// Initializes a new instance of the <see cref="FrameRateCounter"/> class.
        /// </summary>
        /// <param name="game">The Game that the game component should be attached to.</param>
        public FrameRateComponent(Game game)
            : base(game)
        {
            _format = new NumberFormatInfo();
            _format.NumberDecimalSeparator = ".";
        }

        /// <summary>
        /// Initializes the component. Override this method to load any non-graphics resources and query for any required services.
        /// </summary>
        public override void Initialize()
        {
            DrawOrder = Int32.MaxValue;
            base.Initialize();
        }

        /// <summary>
        /// Called when graphics resources need to be loaded. Override this method to load any component-specific graphics resources.
        /// </summary>
        protected override void LoadContent()
        {
            _spriteFont = Game.Content.Load<SpriteFont>(@"Fonts\Default");
            _spriteBatch = new SpriteBatch(GraphicsDevice);
            
            base.LoadContent();
        }

        /// <summary>
        /// Called when graphics resources need to be unloaded. Override this method to unload any component-specifc graphics resources.
        /// </summary>
        protected override void UnloadContent()
        {
            _spriteBatch.Dispose();
            base.UnloadContent();
        }

        /// <summary>
        /// Called when the GameComponent needs to be updated.  Override this method with component-specific update code.
        /// </summary>
        /// <param name="gameTime">Time elapsed since the last call to Microsoft.Xna.Framework.GameComponent.Update(Microsoft.Xna.Framework.GameTime)</param>
        public override void Update(GameTime gameTime)
        {
            _elapsedTime += gameTime.ElapsedGameTime;

            if (_elapsedTime > TimeSpan.FromSeconds(1))
            {
                _elapsedTime -= TimeSpan.FromSeconds(1);
                _frameRate = _frameCounter;
                _frameCounter = 0;
            }

            _frameCounter++;
        }

        /// <summary>
        /// Called when the DrawableGameComponent needs to be drawn.  Override this method with component-specific drawing code.
        /// </summary>
        /// <param name="gameTime">Time passed since the last call to Microsoft.Xna.Framework.DrawableGameComponent.Draw(Microsoft.Xna.Framework.GameTime).</param>
        public override void Draw(GameTime gameTime)
        {
            string fps = string.Format(_format, "FPS: {0:F}\nFT: {1}", _frameRate, gameTime.ElapsedGameTime.Milliseconds);

            _spriteBatch.Begin();
            _spriteBatch.DrawString(_spriteFont, fps, Position + new Vector2(1.0f, 1.0f), Color.Black);
            _spriteBatch.DrawString(_spriteFont, fps, Position, Color.White);
            _spriteBatch.End();
        }
    }
}